/*
 * @Author       : lovefc
 * @Date         : 2021-05-19 13:33:19
 * @LastEditTime : 2021-06-29 10:55:44
 */

//window.location.protocol+'//'+domain+window.location.pathname;

const Storage = {}

Storage.get = function (name) {
    return JSON.parse(localStorage.getItem(name))
}

Storage.set = function (name, val) {
    localStorage.setItem(name, JSON.stringify(val))
}

Storage.add = function (name, addVal) {
    let oldVal = Storage.get(name)
    let newVal = oldVal.concat(addVal)
    Storage.set(name, newVal)
}

function page() {
    window.location.href = window.location.href;
}

/*
$(function(){ 
     $.ajax({ 
          url: './index.html',
          type:'post', 
          data:'name=ZXCVB', 
          timeout:15000, 
          beforeSend:function(XMLHttpRequest){ 
		    $('.commentwrap').hide();
		    $('.comments-list-title').hide();
			$('.fc-pagination').hide();
		    $(this).show(); 
         }, 
         success:function(data,textStatus){ 
             console.log('开始回调'); 
             $("#loading").hide(); 
           }, 
          complete:function(XMLHttpRequest,textStatus){ 
              // alert('远程调用成功，状态文本值：'+textStatus); 
             $("#loading").hide(); 
           },
       }); 
}); 
*/

// 评论事件
function discuss() {
    $("input[name='nowurl']").val(window.location.href);
    $('.discuss').on('click', function (event) {
        //阻止默认提交事件
        event.preventDefault();
        var data = $('#discuss_form').serializeArray();
        $.post("/disuss.html", data,
            function (data, status) {
                if (data.code == 1) {
                    $.notifyBar({
                        cssClass: "error",
                        html: data.msg
                    });
                } else {
                    $.notifyBar({
                        cssClass: "success",
                        html: data.msg
                    });
                    page();
                }
            });
    });
    $('.hf_discuss').on('click', function (event) {
        event.preventDefault();
        var id = $(this).attr('data-id');
        var data = $('#hf_discuss_form_' + id).serializeArray();
        $.post("/disuss.html", data,
            function (data, status) {
                if (data.code == 1) {
                    $.notifyBar({
                        cssClass: "error",
                        html: data.msg
                    });
                } else {
                    $.notifyBar({
                        cssClass: "success",
                        html: data.msg
                    });
                    page();
                }
            });
    });
}

function common() {
    $('.article-image').map(async function (i, n) {
        let path = $(n).attr('src');
        let start = $(n).attr('start');
        let end = $(n).attr('end');
        if (!start) start = 0;
        if (!end) end = 2;
        let image = new Image();
        image.src = path;
        image.onload = function () {
            let palette = new Palette(image);
            let arr = [palette.getVibrantColor(), palette.getLightVibrantColor(), palette
                .getDarkVibrantColor(), palette.getMutedColor(), palette.getLightMutedColor(),
                palette.getDarkMutedColor()
            ];
            let r = arr.filter(function (s) {
                return s && s.trim();
            });
            let arr2 = r.slice(start, 2);
            arr2[0] = arr2[0] + ' 85%';
            let ys = arr2.join(',');
            let sty = `linear-gradient(to top,#f8f8f8,${ys})`;
            let fid = $(n).parent().parent().parent();
            fid.css({
                "background-image": sty
            });
        };
    });
    discuss();
}
common();

$(document).pjax('a[target!=_blank]', '.mdl-layout__content', {
    fragment: '.mdl-layout__content',
    timeout: 5000
});
$(document).on('pjax:complete', function () {
    common();
});
$(document).on('pjax:start', function () {
    NProgress.start();
});
$(document).on('pjax:end', function () {
    NProgress.done();
});